package com.example.qxapp.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.qxapp.Bean.Post;
import com.example.qxapp.Bean.User;
import com.example.qxapp.R;
import com.example.qxapp.adapter.syadapter;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;

public class Fragment1 extends androidx.fragment.app.Fragment {
    private syadapter syadapter;
    private RecyclerView rv;
    private SwipeRefreshLayout srlayout;
    private TextView hellohome,user1,ok;
    private List<Post> data;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tab_sy,container,false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        //逻辑处理
        super.onActivityCreated(savedInstanceState);
        init();
        Bmob.initialize(getActivity(),"9451c76164239204358c92dfe392808d");
        refresh();//初始刷新
        srlayout.setColorSchemeResources(android.R.color.holo_green_light,android.R.color.holo_red_light,android.R.color.holo_blue_light);
        srlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //刷新
                refresh();
            }
        });

        //user加载
        BmobUser bu=BmobUser.getCurrentUser(User.class);
        String userid=bu.getObjectId();
        BmobQuery<User> us=new BmobQuery<>();
        us.getObject(userid, new QueryListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if (e==null){
                    user1.setText(user.getUsername());
                }else {
                    user1.setText("网络错误 ");
                    ok.setText("加载失败！");
                }
            }
        });

    }

    private void refresh() {
        BmobQuery<Post> po=new BmobQuery<Post>();
        po.order("-createAt");
        po.setLimit(1000);
        po.findObjects(new FindListener<Post>() {
            @Override
            public void done(List<Post> list, BmobException e) {
                srlayout.setRefreshing(false);
                if (e==null){
                    data=list;
                    syadapter=new syadapter(getActivity(),data);
                    rv.setLayoutManager(new LinearLayoutManager(getActivity()));
                    rv.setAdapter(syadapter);
                }else {
                    srlayout.setRefreshing(false);
                    Toast.makeText(getActivity(),"获取数据失败"+e,Toast.LENGTH_SHORT).show();
                }
            }
        });
    }



    private void init(){
        user1=getActivity().findViewById(R.id.user);
        rv=getActivity().findViewById(R.id.recycler);
        srlayout=getActivity().findViewById(R.id.swipe);
        hellohome=getActivity().findViewById(R.id.hellohome);
        ok=getActivity().findViewById(R.id.ok11);
    }
}
